package com.erqi.dao.impl;

import com.erqi.dao.userDao;
import com.erqi.mapper.master.PlcuserMapper;
import com.erqi.mapper.master.UserPlcMapper;
import com.erqi.pojo.Message;
import com.erqi.pojo.Plcuser;
import com.erqi.pojo.UserPlc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * @Author king
 * @Date 2020/7/20 15:11
 * @Version 1.0
 */
@Service
public class userDaoImpl implements userDao {

    @Autowired
    PlcuserMapper userMapper;
    @Autowired
    HttpSession session;
    @Autowired
    Message message;
    @Autowired
    UserPlcMapper PlcMapper;
    /**
     * 根据name查询整个entry
     * @param username
     * @return
     */
    public Integer login(String username , String password,String kt){
        Plcuser user = userMapper.selectByUser(username);
        if (user==null){
            return 1;//判断0 user为空
        }
        if (user.getPassword().equals("")&&user.getName().equals("")) {
            return 2;//判断2密码姓名有问题
        }
        if (user.getPassword().equals(password)){
            switch (kt){
                case "1K":
                    message.setOneKUser(user.getName());
                    save("1k",user.getName());
                break;
                case "1T":
                    message.setOneTUser(user.getName());
                    save("1t",user.getName());
                    break;
                case "2K":
                    message.setTwoKUser(user.getName());
                    save("2k",user.getName());
                    break;
                case "2T":
                    message.setTwoTUser(user.getName());
                    save("2t",user.getName());
                    break;
                case "3K":
                    message.setThreeKUser(user.getName());
                    save("3k",user.getName());
                    break;
                case "3T":
                    message.setThreeTUser(user.getName());
                    save("3t",user.getName());
                    break;
                case "4K":
                    message.setFourKUser(user.getName());
                    save("4k",user.getName());
                    break;
                case "4T":
                    message.setFourTUser(user.getName()) ;
                    save("4t",user.getName());
                    break;
                case "Monitor":
                    message.setFourTUser(user.getName()) ;
                    save("Monitor",user.getName());
                    break;
                default:
                    return 6;
            }
            return 0;//登录成功
        }else{
            return 3;//密码错误
        }

    }

    /**
     * 封装保存操作，不用每次都创建对象
     * @param type
     * @param name
     */
    public void save(String type ,String name){
        UserPlc user = new UserPlc();
        user.setType(type);
        user.setName(name);
        PlcMapper.updateByType(user);
    }
}
